package com.uplift.mobile.realty.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.uplift.mobile.rate.service.IRateService;
import com.uplift.mobile.realty.model.RealtyInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.uplift.common.base.ServiceReslut;
import com.uplift.mobile.realty.dao.RealtyLeaseMapper;
import com.uplift.mobile.realty.dao.RealtyRoomMapper;
import com.uplift.mobile.realty.model.RealtyALModel;
import com.uplift.mobile.realty.model.RealtyLease;
import com.uplift.mobile.realty.model.RealtyRoomModel;
import com.uplift.mobile.realty.service.IRealtyLeaseService;

@Service
@Transactional
public class RealtyLeaseServiceImpl implements IRealtyLeaseService {

	@Autowired
	private RealtyLeaseMapper realtyLeaseMapper;
	@Autowired
	private RealtyRoomMapper realtyRoomMapper;
	@Autowired
	private IRateService rateService;

	@Override
	public List<Map<String, Object>> queryRealtyLease(RealtyLease realtyLease) {
		PageHelper.startPage(realtyLease.getPage(), realtyLease.getLimit());
		return this.realtyLeaseMapper.selectRealtyLease(realtyLease);
	}

	@Override
	public ServiceReslut addRealtyLease(RealtyLease realtyLease) {

//		Map<String, Object> sessionUser = SecurityContext.getSessionUser();
//		if(sessionUser == null) {
//			return ServiceReslut.newServiceReslut(0, "获取登录用户信息失败");
//		}
//
//		realtyLease.setCreateUserId((String) sessionUser.get("id"));

		//验证房间状态
		if(StringUtils.isBlank(realtyLease.getRoomId())) {
			return ServiceReslut.newServiceReslut(0, "房间id为空");
		}

		RealtyRoomModel realtyRoomModel = new RealtyRoomModel();
		realtyRoomModel.setId(realtyLease.getRoomId());
		List<Map<String, Object>> maps = realtyRoomMapper.selectRealtyRoom(realtyRoomModel);
		if(maps==null || maps.size()<0) {
			return ServiceReslut.newServiceReslut(0, "获取房间失败");
		}

		if(!StringUtils.equals(maps.get(0).get("state").toString(), "1")) {
			return ServiceReslut.newServiceReslut(0, "房间不为空房状态，不能添加租约");
		}

		realtyLease.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		realtyLease.setState("1");
		realtyLease.setCreateTime(new Date());

		realtyRoomModel.setState("2");		//将房间状态设置为租赁中状态
		realtyRoomModel.setUpdateTime(new Date());
		realtyRoomMapper.updateRealtyRoom(realtyRoomModel);

		//广告处理
		handleAds(realtyLease);

		return ServiceReslut.newServiceReslut(realtyLeaseMapper.insertRealtyLease(realtyLease), "");
	}

	@Override
	public ServiceReslut updateRealtyLease(RealtyLease realtyLease) {
		//广告处理
		handleAds(realtyLease);

		realtyLease.setUpdateTime(new Date());
		return ServiceReslut.newServiceReslut(realtyLeaseMapper.updateRealtyLease(realtyLease),"");
	}

	@Override
	public ServiceReslut deleteRealtyLease(RealtyLease realtyLease) {
		realtyLease.setState("0");		//删除状态
		realtyLease.setUpdateTime(new Date());
		return ServiceReslut.newServiceReslut(realtyLeaseMapper.updateRealtyLease(realtyLease),"");
	}

	@Override
	public ServiceReslut endRealtyLease(RealtyLease realtyLease) {
		List<Map<String, Object>> maps = realtyLeaseMapper.selectRealtyLease(realtyLease);
		if(maps==null || maps.size()<=0) {
			return ServiceReslut.newServiceReslut(0,"查询租约信息失败！");
		}

		//更新关联房间状态为空房状态
		RealtyRoomModel realtyRoomModel = new RealtyRoomModel();
		realtyRoomModel.setId(maps.get(0).get("realtyId").toString());
		realtyRoomModel.setState("1");		//将房间设置为空房状态
		realtyRoomModel.setUpdateTime(new Date());
		realtyRoomMapper.updateRealtyRoom(realtyRoomModel);

		realtyLease.setState("2");		//将租约状态设置为已结束状态
		realtyLease.setUpdateTime(new Date());
		return ServiceReslut.newServiceReslut(realtyLeaseMapper.updateRealtyLease(realtyLease),"");
	}

	@Override
	public List<Map<String, Object>> queryRealtyLeaseAd(String realtyLeaseId) {

		if(StringUtils.isBlank(realtyLeaseId)) {
			return null;
		}
		RealtyLease realtyLease = new RealtyLease();
		realtyLease.setId(realtyLeaseId);

		List<Map<String, Object>> maps = realtyLeaseMapper.selectRealtyLeaseAd(realtyLease);
		handAdUrl(maps);		//处理图片路径

		return maps;
	}

	/**
	 * 处理广告url
	 * @param leaseAds
	 */
	public void handAdUrl(List<Map<String, Object>> leaseAds) {
		if(leaseAds==null || leaseAds.size()==0) {
			return;
		}

		for (int i = 0; i < leaseAds.size(); i++) {
			if(leaseAds.get(i).get("picturePath") != null) {
				leaseAds.get(i).put("picturePath", leaseAds.get(i).get("picturePath"));
			}
		}
	}

	@Override
	public Map<String, Object> getRealtyLease(RealtyLease realtyLease) {

		realtyLease.setState("1");		//租赁中
		getRate(realtyLease);
		List<Map<String, Object>> lease = realtyLeaseMapper.selectRealtyLease(realtyLease);

		if(lease==null || lease.size()==0) {
			return null;
		}
		
		Map<String, Object> leaseMap = lease.get(0);

		leaseMap.put("ads", queryRealtyLeaseAd(leaseMap.get("id").toString()));		//广告列表
		return leaseMap;
	}

	/**
	 * 处理广告信息
	 * @param realtyLease
	 */
	private void handleAds(RealtyLease realtyLease) {
		RealtyALModel realtyALModel = new RealtyALModel();
		realtyALModel.setLeaseId(realtyLease.getId());
		realtyLeaseMapper.deleteLeaseAd(realtyALModel);			//删除租约关联广告

		if(StringUtils.isBlank(realtyLease.getAds())) {
			return;
		}

		String[] ad = StringUtils.split(realtyLease.getAds(), ",");
		for (String s : ad) {
			realtyALModel.setAdId(s);
			realtyLeaseMapper.insertRealtyLease(realtyLease);
		}
	}

	/**
	 *  获取汇率
	 * @param realtyLease
	 * @return
	 */
	private BigDecimal getRate(RealtyLease realtyLease) {
		BigDecimal rate = null;
		if(!StringUtils.isBlank(realtyLease.getMoneyTypeCode()) && !StringUtils.equals(realtyLease.getMoneyTypeCode(), "JPY")) {
			Map<String, Object> rateMap = rateService.queryMoenyRate(realtyLease.getMoneyTypeCode());
			if(rateMap != null) {
				if(rateMap.get("rate") != null) {
					rate = (BigDecimal) rateMap.get("rate");
					realtyLease.setRate(rate);
				}
			}
		} else {
			rate = new BigDecimal(1);
			realtyLease.setRate(rate);
		}

		if(rate == null) {
			return null;
		}

		return rate;
	}

}
